<?php
include("utils.php");
//Start for custom ads ////////////////////////
$all = array();
	//retrieving category Name from categoryName using site's adcategoryId
$query_retrieve_category_name = "select categoryName from adcategories where adcategoryId = 1 ";// let say adcategoryId = 1
$res_retrieve_category_name   = mysql_query($query_retrieve_category_name);
$row_retrieve_category_name   = mysql_fetch_array($res_retrieve_category_name);

	// retrieving all data from adgroup table for custom type

$query_retrieve_adgroup = "SELECT * from adgroup where network_type = 'custom' ";
$res_retrieve_adgroup   = mysql_query($query_retrieve_adgroup);


while($row_retrieve_adgroup = mysql_fetch_array($res_retrieve_adgroup))
{
$query_retrieve_category = "SELECT * from create_network where network_id = ".$row_retrieve_adgroup['adgroup_network']." "; 

//echo "Q-->".$query_retrieve_category."<br>";

$res_retrieve_category   = mysql_query($query_retrieve_category);

if(mysql_num_rows($res_retrieve_category)>0)   {

$row_retrieve_category   = mysql_fetch_array($res_retrieve_category);

//echo "HA--".$row_retrieve_category['category']."<br>";
$arr = array();
$arr = explode(",",$row_retrieve_category['category']);
$check = 0;
	for($i=0;$i<sizeof($arr);$i++)
	{
		//echo "Arr--->".$arr[$i]."<br>";
		if($row_retrieve_category_name['categoryName'] != '' && $row_retrieve_category_name['categoryName'] == $arr[$i])
		{
			$check = 1;
		}
		
		
	}
//echo "Network Id -- >".$row_retrieve_category['network_id']."<br>";
if($check == 1)
{
$all[] = $row_retrieve_category['network_id'];
}
						}

//echo "<br><br><br><br><br><br><br>";
}

for($j=0;$j<sizeof($all);$j++)
{

$query_ag = "Select * from adgroup where adgroup_network = ".$all[$j]." ";
$res_ag   = mysql_query($query_ag);
$row_ag   = mysql_fetch_array($res_ag);

//echo "Adgroup Id---->".$row_ag['adgroup_id']."<br>";

$query_cam = "select * from adcampaign where campaign_id = ".$row_ag['campaign_id']." and start_date <= CURDATE() and end_date >= CURDATE() ";
//echo "Camp Q--->".$query_cam."<br>";
$res_cam   = mysql_query($query_cam);
$row_cam   = mysql_fetch_array($res_cam);

if(mysql_num_rows($res_cam)>0)
{
	$q = mysql_query("SELECT * from adgroup_ad where adgroup_id = ".$row_ag['adgroup_id']." ") ;
	
	if(mysql_num_rows($q)>0)
	{
	$r = mysql_fetch_array($q);


?>



		<table width="250" border="1" cellspacing="0" cellpadding="0" bgcolor="<?php echo $backgroundColor;?>" onClick="javascript:_aClick(<?php echo $ROW[advertiseId];?>);">
              <tr>
                <td align="center">Heading <?php echo $r['heading'] ;?></td>
                <td style="padding-left:20px;"><?php echo $r['personal_reference']; ?></td>
              </tr>
		</table>


<?php

	}

}

}
//////End for custom ad


// Start of DIRECT ADS ///////////////////////////////

echo "<br><br>";
echo "DIRECT ADS<br>";
$query = "SELECT * from adspace_master where adsiteId = 1";
//echo $query;
$res   = mysql_query($query);

while($row = mysql_fetch_array($res))
{

echo "<br>";
$query1 = "SELECT * from adspace_details where adspaceId = ".$row['adspaceId']." ";
$res1   = mysql_query($query1);
$row1 = mysql_fetch_array($res1);
echo "<br>Adspace id--->".$row['adspaceId'];
$query2 = "SELECT * from network_master where networkType = '".$row1['adspaceType']."' ";
$res2   = mysql_query($query2);
while($row2 = mysql_fetch_array($res2))
{
$query3 = "SELECT * from adgroup where adgroup_network = '".$row2['networkId']."' and network_type = 'direct' ";
$res3   = mysql_query($query3);
$row3   = mysql_fetch_array($res3);


$query4 = "select * from adcampaign where campaign_id = ".$row3['campaign_id']." and start_date <= CURDATE() and end_date >= CURDATE() ";
//echo "Camp Q--->".$query_cam."<br>";
$res4   = mysql_query($query4);
$row4   = mysql_fetch_array($res4);

if(mysql_num_rows($res4)>0)
{
	$query5 = "SELECT * from adgroup_ad where adgroup_id = '".$row3['adgroup_id']."' ";
	$res5   = mysql_query($query5);
	if(mysql_num_rows($res5)>0)
	{
	$row5   = mysql_fetch_array($res5);
	?>
	<table width="250" border="1" cellspacing="0" cellpadding="0" bgcolor="<?php echo $backgroundColor;?>" onClick="javascript:_aClick(<?php echo $ROW[advertiseId];?>);">
              <tr>
                <td align="center">Heading <?php echo $row5['heading'] ;?></td>
                <td style="padding-left:20px;"><?php echo $row5['personal_reference']; ?></td>
              </tr>
		</table>
	<?php
	
	}
}



//echo "<br>".$row2['networkId'];
}



}

// End   of DIRECT ADS ///////////////////////////////



?>